﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.sysDAL;
using dhSystemBLL.basicData;
using dhSystemBLL.produceBLL;

namespace dhSystemBLL.orderData
{
   public  class scrlBLL
    {
       private scrlMasterTableAdapter _scrMasterlAdapter = null;
       private scrlMasterTableAdapter scrlMasterAdapter
        {
            get
            {
                if (_scrMasterlAdapter == null) _scrMasterlAdapter  = new scrlMasterTableAdapter();
                return _scrMasterlAdapter ;

            }
        }
       private scrlDetailTableAdapter _scrlDetailAdapter = null;
       private scrlDetailTableAdapter scrlDetailAdapter
       {
           get
           {
               if (_scrlDetailAdapter == null) _scrlDetailAdapter = new scrlDetailTableAdapter();
               return _scrlDetailAdapter;

           }
       }
        /// <summary>
        /// 查询生产领料主表
        /// </summary>
        /// <returns></returns>

       public orderDataSet.scrlMasterDataTable   getscrlMasterAll()
        {
            try
            {
                return scrlMasterAdapter.GetDatascrlMasterALL ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 查询生产领料从表
       /// </summary>
       /// <returns></returns>

       public orderDataSet.scrlDetailDataTable  getscrlDetailAll()
       {
           try
           {
               return scrlDetailAdapter.GetDatascrlDetailALL();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 根据领料单号查询生产领料表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public orderDataSet.scrlMasterDataTable getscrlMasterAllBybillNo(string billNo)
        {
            try
            {
                return scrlMasterAdapter.GetDatascrlMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据领料单号查询生产领料从表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.scrlDetailDataTable  getscrlDetailAllBybillNo(string billNo)
       {
           try
           {
               return scrlDetailAdapter .GetDatascrlDetailBybillNo (billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 修改领料表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int scrlInsert(scrlMasterModel row, orderDataSet.scrlDetailDataTable pTable, string state, string billNo, string Date)
        {
            scrlDetailModel scrlDetailRow = new scrlDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                scrlMasterDAL scrlMaster = new scrlMasterDAL(tn, conn);
                scrlDetailDAL scrlDetail = new scrlDetailDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                if (state == "新增")
                {
                    
                    orderDataSet.scrlMasterDataTable scrlTable = getscrlMasterAllBybillNo (billNo);
                    if (scrlTable.Rows.Count == 0)
                    {
                        scrlMaster.scrlMasterInsert(row);
                    }
                    else
                    {
                        row.billNo = "O" + Date + "-" + basicSql.getOrderSerail("领料单序列号");
                        scrlMaster.scrlMasterInsert(row);
                    }
                    //更新单据编号的过程 
                    //更新订单序列号
                    string orderSerial = basicSql.getOrderSerail("领料单序列号");
                    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "领料单序列号");
                   // scrlMaster.scrlMasterInsert(row);
                    //更新单据编号的过程 
                }
                if (state == "编辑")
                {
                    scrlMaster.scrlMasterUpdate(row);
                }


                foreach (orderDataSet.scrlDetailRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                        scrlDetailRow.recordid = detailRow.recordid;
                        scrlDetailRow.billNo = billNo ;
                        scrlDetailRow.matNum = detailRow.matNum;
                        scrlDetailRow.matName = detailRow.matName ;
                        if (!detailRow.IsmatSpecNull())
                        {
                            scrlDetailRow.matSpec = detailRow.matSpec;
                        }
                        else { scrlDetailRow.matSpec = ""; }
                        scrlDetailRow.qty = detailRow.qty;
                        scrlDetailRow.unit = detailRow.unit;
                        scrlDetailRow.createDate = DateTime.Now;
                        if (!detailRow.IsmarkNull())
                        {
                            scrlDetailRow.mark = detailRow.mark;
                        }
                        else { scrlDetailRow.mark = ""; }
                    }

                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        scrlDetail.scrlDetailInsert(scrlDetailRow);

                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        scrlDetail.scrlDetailUpdate(scrlDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        scrlDetail.scrlDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }


                }

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个领料单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        
        
        /// 删除一行领料表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int scrlMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                scrlMasterDAL scrlMaster = new scrlMasterDAL(tn, conn);
                scrlMaster.scrlMasterDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
